﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Forums.OfflineStore;
using System.Windows.Threading;
using Threading = System.Threading;

namespace Microsoft.Forums.ForumsClient.Models
{
    public class ForumFolderViewModel : BindableObject, IEquatable<ForumFolderViewModel>, IFolderViewModel
    {
        ThreadedForumViewModel threadedForumViewModel;

        /// <summary>
        ///  Hold sync in progress state
        /// </summary>
        bool isSyncInProgress;

        /// <summary>
        /// The name of the folder
        /// </summary>
        /// <value>The name of the folder.</value>
        public string Name
        {
            get
            {
                return threadedForumViewModel.Forum.DisplayName;
            }
        }

        /// <summary>
        /// Gets the threaded forum view model.
        /// </summary>
        /// <value>The threaded forum view model.</value>
        public ThreadedForumViewModel ThreadedViewModel
        {
            get
            {
                return threadedForumViewModel;
            }
        }


        /// <summary>
        ///  Get / Set Synchronization in progress for the forum represented by this forumcontainer 
        /// </summary>
        public bool IsSyncInProgress
        {
            get
            {
                return isSyncInProgress;
            }
            set
            {
                isSyncInProgress = value;
                OnPropertyChanged("IsSyncInProgress");
            }
        }


        /// <summary>
        /// Gets or sets the forum.
        /// </summary>
        /// <value>The forum.</value>
        public Forum Forum
        {
            get;
            private set;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ForumFolderViewModel"/> class.
        /// </summary>
        /// <param name="forum">The forum.</param>
        /// <param name="dispatcher">The dispatcher.</param>
        public ForumFolderViewModel(Forum forum, Dispatcher dispatcher)
        {
            if (forum == null) throw new ArgumentNullException("forum", "forum must not be null");
            if (dispatcher == null) throw new ArgumentNullException("dispatcher", "dispatcher must not be null");

            Forum = ObjectRegistry.GetForum(forum);
            threadedForumViewModel = new ThreadedForumViewModel(Forum, dispatcher);
            
        }

        /// <summary>
        /// Refreshed the unread  count directly from the database
        /// </summary>
        public void RefreshPostCounts()
        {
            Threading.ThreadPool.QueueUserWorkItem(
                 delegate
                 {
                     using (var store = AppDataModel.Instance.ReadOnlyDataContext)
                     {
                         Forum.UnreadPostCount = store.Posts.Count(p => p.ForumId == Forum.Id && p.PostPropertySet.IsRead == false);
                     }
                 });
        }


        #region IEquatable<ForumContainer> Members
        /// <summary>
        /// Determines whether the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <param name="obj">The <see cref="T:System.Object"/> to compare with the current <see cref="T:System.Object"/>.</param>
        /// <returns>
        /// true if the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>; otherwise, false.
        /// </returns>
        /// <exception cref="T:System.NullReferenceException">The <paramref name="obj"/> parameter is null.</exception>
        public override bool Equals(object obj)
        {
            ForumFolderViewModel fc = obj as ForumFolderViewModel;
            if (fc == null) return false;

            return Equals(fc);
        }

        /// <summary>
        /// Serves as a hash function for a particular type.
        /// </summary>
        /// <returns>
        /// A hash code for the current <see cref="T:System.Object"/>.
        /// </returns>
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }



        /// <summary>
        /// Indicates whether the current object is equal to another object of the same type.
        /// </summary>
        /// <param name="other">An object to compare with this object.</param>
        /// <returns>
        /// true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.
        /// </returns>
        public bool Equals(ForumFolderViewModel other)
        {
            if (other == null) return false;

            return other.Forum.Id == this.Forum.Id;
        }

        #endregion

        /// <summary>
        /// Returns a <see cref="System.String"/> that represents this instance.
        /// </summary>
        /// <returns>
        /// A <see cref="System.String"/> that represents this instance.
        /// </returns>
        public override string ToString()
        {
            return Name;
        }
    }
}
